<template>
  <div>
    <el-dialog
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="600px">
      <el-form 
        label-position="right" 
        :model="selectTable" 
        ref="addForm" 
        label-width="80px" 
        autocomplete="off"
        size="medium">
        
        <el-form-item label="名称" prop="classifyName" :rules="[rule[0]]">
            <el-input v-model="selectTable.classifyName" placeholder="请输入" maxLength="20"></el-input>
        </el-form-item>
        
				<el-row>
        	<el-col>
		        <el-form-item label="图片" prop="imgUrl">
		          <singleImage v-model="selectTable.imgUrl"></singleImage>
		        </el-form-item>
        	</el-col>
        </el-row>
        
        <el-form-item label="状态" prop="status" :rules ="[rule[0]]">
          <el-switch
            v-model="selectTable.status"
            active-text="启用"
            :active-value="0"
            inactive-text="停用"
            :inactive-value="1"
            >
          </el-switch>
        </el-form-item>
        
        <el-form-item label="备注" prop="classifyName">
            <el-input type="textarea" v-model="selectTable.comment" placeholder="请输入" maxLength="100"></el-input>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {cateSave,cateModify} from '@/utils/weApi'
import singleImage from '@/components/Upload/singleImage'
export default {
  components: {
    singleImage
  },
  props: {
    data: Object
  },
  created () {
    this.selectTable =  this.JSONo({},this.data)
  },
  watch: {
    show (newVal) {
      this.$parent.edit = false;
    }
  },
  computed: {
    title () {
      if (this.selectTable.method==='edit') {
        return '修改';
      }
      else if (this.selectTable.method==='add') {
        return '添加';
      }
    }
  },
  data () {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'blur' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate();
      try {
        this.pload = true
        if (this.selectTable.method==='edit') {
          delete this.selectTable.children
          delete this.selectTable.parent
          await cateModify(this.selectTable)
        }
        else if (this.selectTable.method==='add') {
          await cateSave(this.selectTable)
        }
        this.$notify({title: '操作成功', type: 'success', duration: 5000})
        this.$parent.edit = false;
        this.$emit('getData');
      } catch(e) {
        this.pload = false
      }
    }
  }
}
</script>